package com.jni.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jni.model.Lnews;
import com.jni.riyada.Article_details;
import com.jni.riyada.R;
import com.jni.utils.ImageLoader;

public class NewsAdapter extends ArrayAdapter<Lnews> {
		private LayoutInflater infalter ;
		private Context context;
		private ArrayList<Lnews> news;
		public ImageLoader imageLoader;
	public NewsAdapter(Context context, ArrayList<Lnews> news) {
		super(context,0,  news);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.news = news;
		infalter = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
		Log.e("ok","inside news adapter");
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		 if (v == null) {
             LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             v = vi.inflate(R.layout.news_item, null);
         }
		v = infalter.inflate(R.layout.news_item, null);
		final Lnews article = news.get(position);
		TextView t = (TextView) v.findViewById(R.id.news_title);
		t.setText(article.article.title);
		((LinearLayout)v.findViewById(R.id.item)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent i = new Intent(context, Article_details.class);
				i.putExtra("articleObject",article.article);
				context.startActivity(i);
				
			}
		});
		
		ImageView image = (ImageView) v.findViewById(R.id.news_image);
		imageLoader.DisplayImage(article.article.picture, image);
		image.getLayoutParams().width = 160;
		image.getLayoutParams().width = 150;
		return v;
		
	}
	

}
